package com.woniuxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.woniuxy.Uitl.OrderUtil;
import com.woniuxy.dto.OrderDTO;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.mapper.SiteMapper;
import com.woniuxy.service.OrderService;
import com.work.framework.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: OrderServiceImpl
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1710:58
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SiteMapper siteMapper;

    private static OrderUtil orderUtil = new OrderUtil();

    @Override
    public Integer addOrder(Order order) {
        //生成订单号
        String orderNum = System.currentTimeMillis() + "";
        order.setOrderNum(orderNum);
        //计算手续费
        BigDecimal rate = new BigDecimal("0.15");
        BigDecimal charges = order.getTransactionPerice().multiply(rate).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal minimum = new BigDecimal("20.00");
        if (charges.compareTo(minimum) == -1) {
            order.setCharges(minimum);
        } else {
            order.setCharges(charges);
        }
        //订单生成时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newdate = new Date();
        String buildTime = simpleDateFormat.format(newdate);
        order.setBuildTime(buildTime);
        //插入订单到数据库
        Integer code = orderMapper.insertOrder(order);
        if (code == 1) {
            //将订单编号放入redis中，不设置过期时间，将生成时间放入redis中
            try {
                redisTemplate.opsForHash().putIfAbsent("订单", orderNum, buildTime);
            } catch (Exception e) {
                redisTemplate.opsForHash().delete("订单", orderNum);
            }
            return 200;
        } else {
            return 500;
        }
    }

    @Override
    public OrderDTO findOrderDTOByOId(Integer oid) {
        OrderDTO orderDTO = orderMapper.findOrderDTOByOid(oid);

        return orderDTO;
    }

    @Override
    public Integer updateOrder(Integer oid, Integer flage) {
        Integer code = orderMapper.updateOrder(oid, flage);
        if (code == 1) {
            return 200;
        } else {
            return 500;
        }
    }

    @Override
    public List<OrderDTO> searchBayerOrderDTO(Integer page, Integer limit, HttpSession session) {
        User user = (User) session.getAttribute("user");
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBid(user.getUid());
        PageHelper.startPage(page, limit);
        List<OrderDTO> orderDTOS = orderMapper.searchBayerOrderDTO(orderDTO);
        orderDTOS = orderUtil.verify(orderDTOS, orderMapper, redisTemplate);
        return orderDTOS;
    }

    @Override
    public List<OrderDTO> searchSellerOrderDTO(Integer page, Integer limit, HttpSession session) {
        User user = (User) session.getAttribute("user");
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setVid(user.getUid());
        PageHelper.startPage(page, limit);
        List<OrderDTO> orderDTOS = orderMapper.searchSellerOrderDTO(orderDTO);
        orderDTOS = orderUtil.verify(orderDTOS, orderMapper, redisTemplate);
        return orderDTOS;
    }

    @Override
    public Map<String, Object> findMassage(Integer oid) {
        Map<String, Object> map = new HashMap<>();
        OrderDTO orderDTO = orderMapper.findOrderDTOByOid(oid);
        Site site = siteMapper.findSiteBySid(orderDTO.getSid());
        map.put("orderDTO", orderDTO);
        map.put("site", site);
        return map;
    }

    @Override
    public Integer addExpressNum(String expressNum, Integer oid) {
        Integer code = orderMapper.addExpressNum(expressNum, oid);
        return code;
    }

    @Override
    @Cacheable(value = "orderDTO")
    public List<OrderDTO> searchOrderDTO(Integer page, Integer limit, OrderDTO orderDTO) {
        OrderDTO dto = orderUtil.addOrderDTO(orderDTO.getOrderNum(), orderDTO.getGname(), orderDTO.getCname());
        PageHelper.startPage(page, limit);
        List<OrderDTO> orderDTOS = orderMapper.searchOrderDTO(dto);
        orderDTOS = orderUtil.verify(orderDTOS, orderMapper, redisTemplate);
        return orderDTOS;
    }

    @Override
    public Integer overOrder(Integer oid) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dealTime = simpleDateFormat.format(date);
        orderMapper.insertdealTime(dealTime);
        Integer code = orderMapper.updateOrder(oid, 3);
        return code;
    }
}
